<?php
	/* -------------------------------------------------------------------
	IOU - expense sharing system 
	Copyright (C) 2009-2013 - Mischa Spelt (m.spelt@gmail.com)
	
	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
	
	------------------------------------------------------------------- */

	include_once("lib/lib.main.php");
	checklogin();
	print_header("IOU");

	define(msgDiv, "<div style='width: 50%; margin: auto auto; margin-bottom: 10px; border: solid 1px #989898; padding: 3px;'>");
	if(!$gUser['Account']) {
		print msgDiv . "<b>No account number entered.</b> You have not entered your bank account number yet. If you enter your account number, users who owe you will see it in their overviews so they can quickly transfer the money to you. You can enter your bank account under <a href='account.php' title='Go to user account page'>User Account</a>.</div>";
	}
	if($gUser['Login'] == $gUser['DisplayName']) {
		print msgDiv . "<b>No display name set.</b> By default, your display name is set equal to your login name. However, you can set a displayed name (for example, your real name) to appear in overviews instead of your login name. You can enter your name under <a href='account.php' title='Go to user account page'>User Account</a>.</div>";
	}

	include_once("lib/groupinfo.php");
	$lastJournalLines = $db->q("table SELECT Bills.ID AS BillID, Groups.ID AS GroupID, Date, Bills.Description AS BillDescription, Journal.Description AS LineDescription, Amount / Divisor AS Amount, IsPayment, IsRepayment, Groups.DisplayName AS GroupName, Users.DisplayName AS UserName FROM Journal LEFT JOIN Bills ON Journal.BillID = Bills.ID LEFT JOIN Groups ON Bills.GroupID = Groups.ID LEFT JOIN Users ON Bills.CreatedBy = Users.ID WHERE UserID = %i ORDER BY Date DESC LIMIT 0, 10", $gUser['ID']);
	print "<h1>Your latest transactions</h1><table class='center'><tr><th>Date</th><th>Description</th><th>Group</th><th>Entered by</th><th>Debt</th><th>Credit</th></tr>";
	$i = 0;
	foreach($lastJournalLines as $line) {
		$color = $line['IsRepayment'] ? "green" : ($line['IsPayment'] ? "blue" : "black");
		print "<tr" . ($i++ % 2 ? " class='gray'" : "") . "><td>{$line['Date']}</td><td><a href='bill.php?id={$line['BillID']}' title='View bill details'>{$line['LineDescription']}</a>" . ($line['LineDescription'] !== $line['BillDescription'] ? "<br/><span style='font-size: 80%'>{$line['BillDescription']}</span>" : "") . "</td><td><a href='group.php?id={$line['GroupID']}' title='View detailed group overview'>{$line['GroupName']}</a></td><td>{$line['UserName']}</td><td style='color: $color;'>" . ($line['Amount'] > 0 ? money_format(moneyFormat, $line['Amount']) : "") . "</td><td style='color: $color;'>" . ($line['Amount'] < 0 ? money_format(moneyFormat, -$line['Amount']) : "") . "</td></tr>";
	}

	print "</table><hr/>";

	$groups = $db->q("column SELECT GroupID FROM Users_Groups LEFT JOIN Groups ON Users_Groups.GroupID = Groups.ID WHERE UserID = %i ORDER BY (GroupID = %i) DESC, DisplayName ASC", $gUser['ID'], $gUser['PreferredGroup']);
	foreach($groups as $groupID) { 
		printGroupInfo($groupID);
		print "<hr>";
	}
	
	print_footer();
?>

